//
//  HRSearchUsersViewController.m
//  Hear
//
//  Created by mac on 2017/6/6.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRSearchUsersViewController.h"
#import "HRSearchUsersViewModel.h"
#import "HRUpdataUserInfoModel.h"
#import "HRBlackListCell.h"
#import "HRDynamicViewController.h"
#import "HRDynamicViewModel.h"
@interface HRSearchUsersViewController ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITextField *searchView;
@property (nonatomic, strong)HRSearchUsersViewModel *viewModel;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *contentArray;
@property (nonatomic, strong)HRDynamicViewModel *attentionModel;

@end

@implementation HRSearchUsersViewController

#pragma mark - Get and Set
- (HRDynamicViewModel *)attentionModel{
    if (_attentionModel == nil) {
        _attentionModel = [[HRDynamicViewModel alloc] init];
    }
    return _attentionModel;
}



-(HRSearchUsersViewModel *)viewModel{
    if (_viewModel == nil){
        _viewModel = [HRSearchUsersViewModel new];
    }
    return _viewModel;
}

-(UITextField *)searchView{
    if (_searchView == nil){
        _searchView = [[UITextField alloc] initWithFrame:CGRectMake(0, 20, kFullWidth-150, 34)];
        _searchView.backgroundColor = [UIColor whiteColor];
        _searchView.delegate = self;
        UIButton *leftImageView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, _searchView.height, _searchView.height)];
        [leftImageView setImage:[UIImage imageNamed:@"搜索-1"] forState:0];;
        _searchView.tintColor = [UIColor blueColor];
        _searchView.leftView = leftImageView;
        _searchView.leftViewMode = UITextFieldViewModeAlways;
        _searchView.placeholder = @"输入ID号/个性签名";
        _searchView.clearButtonMode = UITextFieldViewModeWhileEditing;
        _searchView.font = CustomUIFont(14);
        _searchView.layer.cornerRadius = 5;
        _searchView.clipsToBounds = YES;
        _searchView.returnKeyType = UIReturnKeySearch;
    }
    return _searchView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupView];

}

- (void)clickBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setupView{
//    UIButton *leftButton = [UIButton buttonWithType:0];
//    leftButton.size = CGSizeMake(20, 20);
//    [leftButton setImage:[UIImage imageNamed:@"Back-Arrow"] forState:UIControlStateNormal];
//    [leftButton addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
//    self.navigationItem.leftBarButtonItem = leftItem;
    
    
    self.navigationItem.titleView = self.searchView;
    
    
    UIButton *searchButton = [UIButton buttonWithType:0];
    searchButton.size = CGSizeMake(40, 34);
    [searchButton setTitle:@"搜索" forState:0];
    searchButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [searchButton addTarget:self action:@selector(rightClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:searchButton];
    self.navigationItem.rightBarButtonItem = rightItem;
    searchButton.layer.cornerRadius = 4;
    searchButton.clipsToBounds = YES;
    
    self.contentArray = [NSMutableArray array];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight = 70;

}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.contentArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    WeakSelf
    
    HRBlackListCell *cell = [HRBlackListCell cellWithTableView:tableView];
    
    HRUpdataUserInfoModel *model = self.contentArray[indexPath.row];
    if (model)
    {
        cell.searchModel = model;
    }
    cell.attentionBlock = ^(){
        [weakSelf attention:model];
    };
    //取消cell的点击状态
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HRUpdataUserInfoModel *mode = self.contentArray[indexPath.row];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HRDynamicViewController *VC = [HRDynamicViewController new];
    VC.currentUserID = mode.LocalId;
    [self.navigationController pushViewController:VC animated:YES];
}




-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:NO];
}

- (void)attention:(HRUpdataUserInfoModel *)infoModel{
    HRSaveUserFollowRequestModel *requestModel = [HRSaveUserFollowRequestModel new];
    requestModel.CurrentUserLocalId = USERID;
    requestModel.FollowedUserLocalId = infoModel.LocalId;
    requestModel.CreateTime = [self getCurrentTime];
    
    WeakSelf
    [self.attentionModel saveUserFollow:requestModel isSeccess:^(BOOL isSeccess) {
        if (isSeccess){
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD showMessage:@"关注成功" finishBlock:nil];
                infoModel.IsFollow = @"1";
                [weakSelf.tableView reloadData];
            });
        }
    }];
}
- (NSString *)getCurrentTime
{
    
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
    
    return [formatter stringFromDate:[NSDate date]];
}


#pragma -mark- UITextFiledDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    
    if (self.searchView.text.length == 0){
        [MBProgressHUD showMessage:@"搜索内容不能为空" finishBlock:nil];
        return YES;
    }
    
    [self getSearchResultUserInfo];
    return YES;
}


- (void)rightClick{
    [self.view endEditing:NO];
    
    if (self.searchView.text.length == 0){
        [MBProgressHUD showMessage:@"搜索内容不能为空" finishBlock:nil];
        return;
    }
    
    [self getSearchResultUserInfo];
}


- (void)getSearchResultUserInfo{
    [self.viewModel getSearchResultUsersInfo:@{@"CurrentUserLocalId":USERID,@"Keywords":self.searchView.text} isSeccess:^(HRUpdataUserInfoModel *userModel) {
        if (userModel){
            [self.contentArray removeAllObjects];
            [self.contentArray addObject:userModel];
            [self.tableView reloadData];
        }
    }];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [self.view endEditing:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
